﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using FRW.Common.Extensions;
using Money.Plugins;

namespace Money.Interface
{
    public partial class CategoryGraphPercentage : GraphControl, IGraph
    {
        public CategoryGraphPercentage()
        {
            InitializeComponent();

            HidePanel();

            Name = "Category Group (%)";
        }

        public override void UpdateGraph(object sender, DateChangedEventArgs e)
        {
            DrawGraph(e.StartDate, e.EndDate);
        }

        private void DrawGraph(DateTime startDate, DateTime endDate)
        {
            var categoryFilter = PredicateBuilder.True<Category>();

            Func<Category, bool> filter = PredicateBuilder.False<Category>().Or(categoryFilter).Compile();

            var categories = from category in Categories.CategoryList.Where(filter) select category;

            List<DateTime> range = startDate.MonthsBetween(endDate).ToList();

            var transactions = from transaction in _dataInterface.Transactions.Where(Transactions.InDateRange(startDate, endDate).Compile()) select transaction;

            var filteredTransactions = from filteredTransaction 
                                       in transactions
                                       where filteredTransaction.Amount <= 0 && filteredTransaction.Type != Transaction.TransactionType.Transfer 
                                       select filteredTransaction;

            var categoryGroups = from t
                        in filteredTransactions
                        orderby t.CategoryGroup ascending
                        group t by t.CategoryGroup into gp
                        select new
                        {
                            Category = gp.Key,
                            Group = gp
                        };

            _graph.Series.Clear();
            _graph.Tag = "Categories";

            foreach (var categoryGroup in categoryGroups)
            {
                var categorySpend =
                    from dr in range
                    join transaction in categoryGroup.Group
                    on dr.StartOfMonth() equals transaction.Date.StartOfMonth()
                    into joinedGroups
                    from joinedGroup in joinedGroups.DefaultIfEmpty(new Transaction() { Date = dr.StartOfMonth() })
                    group joinedGroup by joinedGroup.Date.StartOfMonth() into jgp
                    select new { Month = jgp.Key, Amount = jgp.Sum(tt => -tt.Amount) };

                Series series = new Series();

                series.ChartArea = "default";
                series.ChartType = SeriesChartType.StackedColumn100;
                series.Legend = "default";
                series.Name = categoryGroup.Category;
                series.SmartLabelStyle.IsOverlappedHidden = false;

                _graph.Series.Add(series);

                // set style of series and add the data set
                //_graph.Series[series.Name]["DrawingStyle"] = "Cylinder";
                _graph.Series[series.Name].Points.DataBind(categorySpend.ToList(), "Month", "Amount", "");
            }

            OnFiltered();
        }
    }
}
